
import { _decorator, assetManager, CCInteger, Component, Label, Node, ProgressBar, resources, Size, size, UITransform, Vec3 } from 'cc';
import { resManger } from '../tools/ResManger';
import EvtManger from '../tools/EvtManger';
import { AppControll, AppState, EventName } from '../manager/AppControll';
import { Loader } from '../tools/LoadManger';
import { GameData } from '../system/GameData';
const { ccclass, property } = _decorator;


@ccclass('load')
export class load extends Component {
    @property(ProgressBar)
    mPRProgressBar: ProgressBar = null;
    @property(Label)
    mLbProgress: Label = null
    @property(Node)
    mNdBullet: Node = null;
    @property(CCInteger)
    mNLength = 538;

    private mCurPorgress: number = 0;
    private PreList: string[] = ["prefab/Fight", "prefab/Monster", "prefab/temporaryBoss", "prefab/Hp"];
    private DriList: string[] = ["image/gun/secondaryGun"];
    private DyBgList: string[] = ["bullet"];
    private DriPreList: string[] = [];
    private mBIsFinish: boolean = false;
    private mBundleCount = 4;
    private mStartN = 0; //记录子弹的初始X轴位置

    protected onLoad(): void {
        this.mStartN = this.mNdBullet.position.x;
        this.mPRProgressBar.progress = 0;
        resManger.InitResManger(() => {
            this.mCurPorgress++
        });

        this.StartLoad();
    }

    StartLoad() {
        assetManager.loadBundle("dynamic", (err, data) => {
            Loader.dynamic = data;
            this.mCurPorgress++;
            Loader.PreLoadEffectImageDir(GameData.mainGunlevelUpAn);
        })

        assetManager.loadBundle("dynamicPrefab", (err, data) => {
            Loader.dynamicPrefab = data;
            this.mCurPorgress++;
            resManger.InitJson(() => {
                this.mCurPorgress++;
            });

            this.DriPreList.forEach(str => {
                Loader.dynamicPrefab.preload(str, (err, data) => {
                    if (err) {
                        console.log(err);
                        return
                    }
                })
            })

        })

        assetManager.loadBundle("dynamicBg", (err, data) => {
            Loader.dynamicBg = data;
            this.mCurPorgress++;

            this.DyBgList.forEach(str => {
                Loader.dynamicBg.preloadDir(str, () => {
                    this.mCurPorgress++;
                })
            })
        })

        this.DriList.forEach(str => {
            resources.preloadDir(str, (err, data) => {
                this.mCurPorgress++;
            });
        })

        this.PreList.forEach(str => {
            resources.load(str, (err, date) => {
                this.mCurPorgress++;
            })
        })
    }

    protected update(dt: number): void {
        
        if (this.mBIsFinish) return;

        let count = this.mCurPorgress / (this.DriList.length + this.PreList.length + this.mBundleCount + this.DyBgList.length);
        this.mLbProgress.string = Math.round(count * 100) + "%"//返回一个四舍五入的整数
        this.mNdBullet.position = new Vec3(this.mStartN + this.mNLength * count, this.mNdBullet.position.y);
        this.mPRProgressBar.progress = count;
        if (count >= 1) {
            this.mBIsFinish = true;
            this.scheduleOnce(() => {
                EvtManger.do(EventName.appStateChange, AppState.ready);
            })
            return;
        }

    }
}
